Lesson 3

This lesson will review linear equations, briefly discuss kinematics and see how we can write functions in python to reuse code.

Linear Equations

Recall the definition of a line is $y(x) = mx + c$. Where $m$ is the slope of the line and $c$ is the y-intercept. See if you can find the equation of the line below.

The slope of the line is $2$ and the y-intercept is $4$, so the equation is $y(x) = 2x+4$. If we are told that a line goes through two points $(x_1, y_1)$ and $(x_2,y_2)$, we can also find the equation of the line. We know that since the slope is defined as rise over run: $m = \frac{y_2-y_1}{x_2-x_1}$. Now that we have found the slope of the line, how can we find the y-interccept? The line goes through $(x_1, y_1)$ so $y_1 = m*x_1 + c$ and hence $c = y_1-m*x_1$. Alternatively, we also know that the line goes through $(x_2, y_2)$ so $y_2 = m*x_2 + c$ and $c = y_2 - m*x_2$. Now we have found both $m$ and $c$ so we know the equation of the line!

Example Problems

  1. What is the slope of the line that goes through the points $(1,2)$ and $(3,4)$?
  2. What is the y-intercept of the line that goes through $(1,2)$ and $(3,4)$?
  3. What is the equation of the line that goes through the points $(1,2)$ and $(3,4)$?

Basic Kinematics

Kinematics is defined as the study of motion and one branch of it looks at the relationships between position, speed, and time.

The position of an object is where it is relative to an origin at a specific moment in time. If your school is two miles away from home, then at this moment your position, relative to your home, is two miles. When you leave school and decide to go home, your position will slowly decrease as you walk home until it reaches zero miles.

The speed of an object is how fast it is moving at a specific moment in time. If you walk from your house to school in one hour, then your speed would have been two miles per hour. If, instead, you decided to bike from home to school and it only took you half an hour, your speed would have been four miles per hour.

If you decide to skip school (please don't) to go the new arcade four miles away from your house, it will take you twice as long because you have to travel twice the distance. If you decide to walk there, it will take you two hours and if you bike there, it will take you one hour.

In general we have the following relationship $position = speed \times time$ or $p(t) = st$. But wait, doesn't this look really similar to our linear equation above? $p(t) = st + 0$ and $y(x) = mx + c$. Its just a linear line with a y-intercept of zero! The same properties hold as well. Since $s$ is the "slope" of this graph, we can calculate $s$ by taking the rise over run of the graph or the change in position divided by the change in time. The time it takes to travel from one one position to another is the change in position divided by the speed.

The equations are summarized below. $\Delta$ is the shorthand notation for "change in".

  1. $p(t) = st$
  2. $s = \frac{\Delta p}{\Delta t}$
  3. $t = \frac{\Delta p}{\Delta s}$

Suppose your friend lives four miles away from school, but in the opposite direction. If he walks just as quickly as you, how long will it take him to get home?

Because he lives four miles away and walks at two miles per hour, it will take him $\frac{4}{2} = 2$ hours. This is one hour after you arrive at your home (it takes you one hour to get home by foot). We define this as the time difference of arrival. However, if you lived four miles away and your friend lived two miles away, he would have gotten home one hour before you have, and the time difference of arrival would be negative (-1 hours). Notice that the time difference of arrival is also how much more distance your friend has to travel over his speed.

Problem

What would be the time difference of arrival if you lived 3 miles away from school and your friend also lived 3 miles away from school?

Functions in Python

In programming, a function refers to a procedure that does something for you! Much like how in mathematics a function takes in an "input" and returns an "output" after performing some operations on the inputs.

Below is a function that returns the double of an input;


In [ ]:
def double(x):
    return(2*x);

Look at some sample outputs below


In [ ]:
print double(1);
print double(2);
print double(3);

We can define a function in python by the def keyword, followed by the name of the function, a set of parenthesis with its inputs or paramaters and a :. The code that modifies our inputs is in the lines that follow. It has the general format of

def name(parameters):
    code;

Look at our definition of double again and notice that we return the value $2x$. We then print out double(1) which prints out the return value $2 \times 1$. However, it is not required for a function to return anything! A function is still valid even it doesn't explicitly return anything. An example is shown below.


In [ ]:
def print_double(x):
    print(2*x);

Whenever we call print_double, we ask the function to print $2x$ so we don't have to.


In [ ]:
print_double(1);
print_double(2);
print_double(3);

A function can also have multiple parameters, or no parameters at all. Remember, the parameters are the things that go in between the paranthesis. Suppose that we want a function that takes in zero parameters and always returns $1337$, we just leave the parameter section blank.


In [ ]:
def leet():
    return 1337;

What do you think will happen if we run

print leet();

Try it below!


In [ ]:
#Try what "print leet()" does.
#Enter your code here:

Recall that the slope of a line that goes through $(x_1, y_1)$ and $(x_2, y_2)$ is $\frac{y_2-y_1}{x_2-x_1}$. Write a function that takes in four parameters x1,y1,x2,y2 and finds the slope of the line that goes through the two points. Call it find_Slope.


In [ ]:
#Write your function here

#Solution
def find_Slope(x1,y1,x2,y2):
    return (y2-y1)/(x2-x1);

Recall the example of you and your friend walking home from school. Now we will write a function that will calculate the time difference of arrival for an abstract sense. If your friend lives a distance $a$ miles away from you and you both decide to hang out at a location $x$ during the weekend, what will be the time difference of arrival of you and your friend getting home? Both of you walk with the same speed $s$. Answer the questions below to help find the TDOA.

  1. How far do you have to walk home?
  2. How long does it take for you to walk home?
  3. How far does your friend have to walk home?
  4. How long will it take him to walk home?
  5. What is the difference in these two times?

Write a python function below that takes in $a$ (friends house), $x$ (hangout location) and $s$ (the speed you two walk) and returns the TDOA. The function declaration should be

def TDOA(a, x, s):

In [ ]:
#Write your function here

#Solution
def TDOA(a, x, s): 
    return (a-2.0*x)/s;

Now we have a function that given a position finds the time difference of arrival. What if we want to find the position of your hangout spot given the TDOA? Write a function called find_position that takes in the position of your friend's house $a$, a time difference of arrival ($t$), and speed ($s$) and returns the position of your hangout location.


In [ ]:
#Write your function here

#Solution
def find_position(a, t, s):
    return (a-s*t)/2.0;

The find_position function you've defined will be vital in the final step of this project.

Problems

  1. What is the TDOA if you decide to hang out at a location to the LEFT of your house?
  2. Will your functions still work if this is the case?